package cn.dnuihyy406.dnuifood_16110100406.controller.collectfragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.controller.foodlist.FoodListAdapter;
import cn.dnuihyy406.dnuifood_16110100406.controller.homefragment.HomeFragment;
import cn.dnuihyy406.dnuifood_16110100406.controller.mefragment.MeFragment;
import cn.dnuihyy406.dnuifood_16110100406.controller.searchfragment.SearchFragment;

public class CollectFragment extends Fragment {
    private Button C_shop,C_food;
    private CShopFragment cShopFragment;
    private CFoodFragment cFoodFragment;
    private View view;

    protected FragmentManager childManager;
    private FragmentTransaction transaction;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.collect_fragment,container,false);
        C_shop=view.findViewById(R.id.c_shop);
        C_food=view.findViewById(R.id.c_food);
        initViews();
        initEvents();
        return view;
    }
    private void initViews(){
        childManager=getChildFragmentManager();
        C_shop=view.findViewById(R.id.c_shop);
        C_food=view.findViewById(R.id.c_food);
        C_shop.setSelected(true);

    }

    private void initEvents(){
        C_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(cShopFragment == null){
                    cShopFragment=new CShopFragment();
                }
                replaceFragment(cShopFragment);
                C_shop.setSelected(true);
                C_food.setSelected(false);
            }
        });

        C_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(cFoodFragment == null){
                    cFoodFragment=new CFoodFragment();
                }
                replaceFragment(cFoodFragment);
                C_shop.setSelected(false);
                C_food.setSelected(true);
            }
        });
    }

    protected void addFragment(Fragment fragment){
        transaction =childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }

    protected void replaceFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        if(cShopFragment==null){
            cShopFragment=new CShopFragment();
            addFragment(cShopFragment);

        }else {
            replaceFragment(cShopFragment);
        }

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
}
